package net.siufung.boot.datasource.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.List;
import java.util.stream.IntStream;

/**
 * @author 陈建峰
 * @since 2022/8/11 6:11 下午
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TableInfo implements Serializable {

    private static final long serialVersionUID = -3559170478630756654L;

    /**
     * 名称
     */
    private String name;
    /**
     * 注释
     */
    private String comment;
    /**
     * 字段明细
     */
    private List<TableField> fieldList;
    /**
     * 字段名称明细
     */
    private List<String> fieldNameList;
    /**
     * 存在主键
     */
    private Boolean existPrimaryKey = false;
    /**
     * 存在多主键
     */
    private Boolean existMultiPrimaryKey = false;

    /**
     * 字段名称集合字符串
     */
    private String fieldNames;

    /**
     * 字段名称集合字符串
     */
    public String getFieldNames() {
        StringBuilder names = new StringBuilder();
        if (!CollectionUtils.isEmpty(fieldNameList)) {
            IntStream.range(0, fieldNameList.size()).forEach(i -> {
                String fieldName = fieldNameList.get(i);
                if (i == fieldNameList.size() - 1) {
                    names.append(fieldName);
                } else {
                    names.append(fieldName).append(", ");
                }
            });
        }
        return names.toString();
    }

}
